package com.ny.service.impl;

import com.ny.entity.Devices;
import com.ny.mapper.DeviceMapper;
import com.ny.service.impl.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public List<Devices> findAll() {
        return deviceMapper.findAll();
    }

    @Override
    public Devices addDevice(Devices device) {
        return deviceMapper.save(device);
    }

    @Override
    public Devices updateDevice(int deviceId, Devices device) {
        Devices existingDevice = deviceMapper.findByDeviceId(deviceId); // 修正方法名
        if (existingDevice != null) {
            // 更新可修改的字段
            existingDevice.setName(device.getName());
            existingDevice.setType(device.getType());
            existingDevice.setStatus(device.getStatus());
            existingDevice.setBrightness(device.getBrightness());
            existingDevice.setRoom(device.getRoom());
            existingDevice.setInstall_time(device.getInstall_time());
            return deviceMapper.save(existingDevice);
        }
        return null;
    }

    @Override
    public boolean deleteDevice(int deviceId) {
        Devices device = deviceMapper.findByDeviceId(deviceId); // 修正方法名
        if (device != null) {
            deviceMapper.deleteByDeviceId(deviceId); // 修正方法名
            return true;
        }
        return false;
    }

    @Override
    public List<Devices> findByKeyword(String name) {
        return deviceMapper.findByNameLike(name);
    }
}